/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.util.translator.sld2owl.owl;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.datatypes.RDFDatatype;
import com.hp.hpl.jena.datatypes.TypeMapper;

/**
 * This class translate the resource element
 *
 */
public class ResourcesTranslator {

    /**
     * Creates a new instance of ResourcesTranslator    
     * @param node the resources node of the Dom structure
     * @param ont The ontology   
     * @param nsName The namespace
     */
    public ResourcesTranslator(Node node, OntModel ont, String nsName) {

        OntClass Resource =
                ont.getOntClass(nsName + "Resource");

        DatatypeProperty identifier =
                ont.getDatatypeProperty(nsName + "identifier");
        DatatypeProperty href =
                ont.getDatatypeProperty(nsName + "href");
        DatatypeProperty type =
                ont.getDatatypeProperty(nsName + "type");
        DatatypeProperty xmlBase =
                ont.getDatatypeProperty(nsName + "xmlbase");

        NodeList resources =
                node.getChildNodes();
        if (resources != null) {
            for (int i =
                    0; i < resources.getLength(); i++) {
                Node reso =
                        resources.item(i);
                if (reso.getNodeName().compareTo("resource") == 0) {
                    try {
                        lastInstanceSLDTranslator lastInst =
                                new lastInstanceSLDTranslator();
                        String instance =
                                "resource_instance_" +
                                (String.valueOf(lastInst.lastInstance(Resource)));

                        Individual instLd =
                                ont.createIndividual(nsName + instance, Resource);
                        NamedNodeMap attrsItem =
                                reso.getAttributes();
                        for (int x =
                                0; x < attrsItem.getLength(); x++) {
                            Node attr =
                                    attrsItem.item(x);
                            if (attr.getNodeName().trim().compareTo("identifier") ==
                                    0) {
                                if (attr.getNodeValue().trim().compareTo("") !=
                                        0) {
                                    TypeMapper tm =
                                            TypeMapper.getInstance();
                                    RDFDatatype overType =
                                            tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                                    RDFNode value =
                                            ont.createTypedLiteral(attr.getNodeValue().
                                            trim(), overType);
                                    instLd.setPropertyValue(identifier, value);
                                }
                            }
                            if (attr.getNodeName().trim().compareTo("type") == 0) {
                                if (attr.getNodeValue().trim().compareTo("") !=
                                        0) {
                                    RDFNode typeValue =
                                            ont.createTypedLiteral(attr.getNodeValue().
                                            trim());
                                    instLd.setPropertyValue(type, typeValue);
                                }

                            }
                            if (attr.getNodeName().trim().compareTo("href") == 0) {
                                if (attr.getNodeValue().trim().compareTo("") !=
                                        0) {
                                    RDFNode hrefValue =
                                            ont.createTypedLiteral(attr.getNodeValue().
                                            trim());
                                    instLd.setPropertyValue(href, hrefValue);
                                }

                            }
                            if (attr.getNodeName().trim().compareTo("xml:base") ==
                                    0) {
                                if (attr.getNodeValue().trim().compareTo("") !=
                                        0) {
                                    RDFNode xmlBaseValue =
                                            ont.createTypedLiteral(attr.getNodeValue().
                                            trim());
                                    instLd.setPropertyValue(xmlBase,
                                            xmlBaseValue);
                                }

                            }
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }

                }            //if

            }
        }//for
    }
}
